import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/user'
    },
    {
      path: '/user',
      component: () => import('../views/user/index.vue')
    },
    {
      path: '/word',
      component: () => import('../views/word/index.vue')
    },
    {
      path: '/category',
      component: () => import('../views/category/index.vue')
    },
    {
      path: '/audio',
      component: () => import('../views/audio/index.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router 